// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcObject.h"
#include "MgcController.h"

MgcImplementRootRTTI(MgcObject);
MgcImplementRootSmartPointers(MgcObject);
MgcImplementRootStream;
MgcImplementRootController(MgcObject);

unsigned int MgcObject::ms_uiNextID = 0;

//---------------------------------------------------------------------------
MgcObject::MgcObject ()
{
    m_uiReferences = 0;
    m_pkControl = 0;
    m_acName = 0;
    m_uiID = ms_uiNextID++;
    ms_uiTotalObjects++;
}
//---------------------------------------------------------------------------
MgcObject::~MgcObject ()
{
    DestroyAllControllers();
    delete[] m_acName;
    ms_uiTotalObjects--;
}
//---------------------------------------------------------------------------
void MgcObject::SetName (const char* acName)
{
    delete[] m_acName;

    if ( acName )
    {
        m_acName = new char[strlen(acName)+1];
        strcpy_s(m_acName,strlen(acName)+1,acName);
    }
    else
    {
        m_acName = 0;
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcObject::Factory (MgcStream& rkStream)
{
    // MgcObject is abstract, Factory never called
    return 0;
}
//---------------------------------------------------------------------------
void MgcObject::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    // get old address of object, save it for linking phase
    MgcObject* pkLinkID;
    MgcStreamRead(rkStream,pkLinkID);
    rkStream.InsertInMap(pkLinkID,pkLink);

    // native data
    rkStream.ReadString(m_acName);

    // link data
    MgcObject* pkControl;
    MgcStreamRead(rkStream,pkControl);
    pkLink->Add(pkControl);
}
//---------------------------------------------------------------------------
void MgcObject::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject* pkLinkID = pkLink->GetLinkID();
    m_pkControl = (MgcController*)rkStream.GetFromMap(pkLinkID);
}
//---------------------------------------------------------------------------
bool MgcObject::Register (MgcStream& rkStream)
{
    if ( rkStream.InsertInMap(this,0) )
    {
        if ( m_pkControl )
            m_pkControl->Register(rkStream);
        return true;
    }

    return false;
}
//---------------------------------------------------------------------------
void MgcObject::Save (MgcStream& rkStream)
{
    // run-time class name for factory lookup, address for unique link ID
    rkStream.WriteString((const char*)GetRTTI()->GetName());
    MgcStreamWrite(rkStream,this);

    // native data
    rkStream.WriteString(m_acName);

    // link data
    MgcStreamWrite(rkStream,m_pkControl);
}
//---------------------------------------------------------------------------
